package com.aerozhonghuan.logisticsmall.framework.hybrid.actions;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.aerozhonghuan.logisticsmall.framework.hybrid.NewWebviewFragment;
import com.aerozhonghuan.logisticsmall.framework.hybrid.SimpleActionHandler;
import com.aerozhonghuan.logisticsmall.framework.hybrid.bean.JsMobileAgentConstants;
import com.aerozhonghuan.logisticsmall.utils.TimeUtil;
import com.aerozhonghuan.logisticsmall.widget.CustomDatePickerDialog;
import com.aerozhonghuan.logisticsmall.widget.CustomTimePickerDialog;
import com.aerozhonghuan.hybrid.XJsCallback;
import org.json.JSONObject;
import java.text.ParseException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述 js掉用哪个时间选择控件
 * 作者 Zhang Yonghui
 * 创建日期 2019/1/19
 */
public class TimePickerActionHandler extends SimpleActionHandler {
    private static final String KEY_PARAMS_TYPE = "type";
    private static final String KEY_PARAMS_TIME = "time";
    private String startDate = "";
    private String type = "";

    public TimePickerActionHandler(NewWebviewFragment fragment) {
        super(JsMobileAgentConstants.FLAG_SHOWTIMEPICKER, fragment);
    }

    @Override
    public void handleAction(@NonNull String action, @Nullable final JSONObject params, @Nullable final XJsCallback callBack) {

        long currentTime = System.currentTimeMillis();
        if (params != null && params.has(KEY_PARAMS_TIME)) {
            startDate = params.optString(KEY_PARAMS_TIME);
        }
        if (params != null && params.has(KEY_PARAMS_TYPE)) {
            type = params.optString(KEY_PARAMS_TYPE);
        }
        if (!TextUtils.isEmpty(startDate)) {
            try {
                currentTime = TimeUtil.stringToLong(startDate, type);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        // 调用系统日期时间控件获取时间
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(currentTime);
        final Map<String, String> resultParams = new HashMap<>();
        new CustomDatePickerDialog(getActivity(), new CustomDatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, final int year, final int month, final int dayOfMonth) {
                    if (!TextUtils.isEmpty(type) && type.contains("HH")) {
                        new CustomTimePickerDialog(getActivity(), new CustomTimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                String replace = type.replace("yyyy", String.valueOf(year))
                                        .replace("MM", String.valueOf(getFormatTime(month + 1)))
                                        .replace("dd", String.valueOf(getFormatTime(dayOfMonth)))
                                        .replace("HH", String.valueOf(getFormatTime(hourOfDay)))
                                        .replace("mm", String.valueOf(getFormatTime(minute)));
                                resultParams.put("time", replace);
                                onResult(resultParams, callBack);
                            }

                        }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();
                    } else {
                        String replace = type.replace("yyyy", String.valueOf(year))
                                .replace("MM", String.valueOf(getFormatTime(month + 1)))
                                .replace("dd", String.valueOf(getFormatTime(dayOfMonth)));
                        resultParams.put("time", replace);
                        onResult(resultParams, callBack);
                    }
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();

    }

    private String getFormatTime(int time) {
        if (time < 10) {
            return String.format("0%s", time);
        } else {
            return String.valueOf(time);
        }
    }
}
